package com.heima.wemedia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.file.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.WmMaterialDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.enums.WmMaterial;
import com.heima.wemedia.Util.WmThreadLocalUtil;
import com.heima.wemedia.dao.WmMaterialDao;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 17:49:42
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Resource
    private WmMaterialDao wmMaterialDao;
    @Autowired
    private MinIOTemplate minIOTemplate;

    @Override
    public ResponseResult uploadPic(MultipartFile multipartFile) throws IOException {
          if(multipartFile==null||multipartFile.getSize()==0){
              return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
          }
        String originalFilename = multipartFile.getOriginalFilename();
        String replace = UUID.randomUUID().toString().replace("-", "");
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        replace=replace+suffix;
        String url = minIOTemplate.uploadImgFile("", replace, multipartFile.getInputStream());


        //3. 保存素材数据到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(Long.valueOf(WmThreadLocalUtil.getUser().getId()));
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);

        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUpdatedTime(new Date());

        wmMaterialDao.insert(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult<WmMaterial> selectList(WmMaterialDto dto) {
       if(dto==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
       }
       dto.checkParam();
        WmMaterial query = new WmMaterial();
        query.setUserId(Long.valueOf(WmThreadLocalUtil.getUser().getId()));
        if (dto.getIsCollection() != null && dto.getIsCollection() == 1) {
            query.setIsCollection(dto.getIsCollection());
        }
       PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<WmMaterial> page = wmMaterialDao.queryAll(query);

        //3. 封装数据返回
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getResult());

        return result;
    }

    @Override
    public ResponseResult deleteById(int id) {
        WmMaterial wmMaterial = wmMaterialDao.queryById(id);
        if(wmMaterial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int i = wmMaterialDao.deleteById(id);
        if(i==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult isCollection(int id) {
        WmMaterial wmMaterial = wmMaterialDao.queryById(id);
        if(wmMaterial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmMaterialDao.isCollection(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult cancelCollect(int id) {
        WmMaterial wmMaterial = wmMaterialDao.queryById(id);
        if(wmMaterial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmMaterialDao.cancelCollect(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
